package com.hooper.api.client;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.hooper.api.util.SignUtil;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2023/04/11/19:44
 */

public final class ApiClient {
    private ApiClient() {
    }

    private String accessKey;
    private String secretKey;

    private static final String GET = "GET";
    private static final String POST = "POST";

    private static final ApiClient INSTANCE = new ApiClient();

    private static Map<String, String> authMap() {
        Map<String, String> params = new HashMap<>();
        params.put("nonce", String.valueOf(new Random().nextInt(1000) * 3));
        params.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
        params.put("accessKey", INSTANCE.accessKey);
        String sign = SignUtil.sign(INSTANCE.secretKey, params.get("timestamp"));
        params.put("sign", sign);
        return params;
    }
    private static final String API_URL = "http://localhost:8888";

    public String call(String url, String requestParams,String method) {
        String res = null;
        if (method.equals(GET)) {
            String baseUrl = API_URL + url;
            if (StringUtils.hasText(requestParams)) {
                baseUrl += "?" + requestParams;
            }
            try {
                res = HttpUtil.createGet(baseUrl)
                        //q: isOverride 这个参数是什么意思？
                        //a: 是否覆盖已有的header
                        .headerMap(authMap(), false)
                        .execute()
                        .body();
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        } else if (method.equals(POST)) {
            try {
               res =  HttpRequest.post(API_URL + url)
                        .headerMap(authMap(), false)
                        .body(requestParams)
                        //q: isOverride 这个参数是什么意思？
                        //a: 是否覆盖已有的header
                        .execute()
                        .body();
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }
        return res;
    }

    public String randomCnName() {
       return HttpUtil.createGet(API_URL + "/name")
               //q: isOverride 这个参数是什么意思？
               //a: 是否覆盖已有的header
               .headerMap(authMap(), false)
               .execute()
               .body();
    }


    public String randomAddr() {
        return HttpUtil.createGet(API_URL + "/addr")
                //q: isOverride 这个参数是什么意思？
                //a: 是否覆盖已有的header
                .headerMap(authMap(), false)
                .execute()
                .body();
    }

    public String randomPhone(String prefix) {
        return HttpUtil.createPost(API_URL + "/addr")
                .headerMap(authMap(), false)
                .body(JSONUtil.toJsonStr(prefix))
                //q: isOverride 这个参数是什么意思？
                //a: 是否覆盖已有的header
                .execute()
                .body();
    }

    public String randomDate(Integer start) {
        return HttpRequest.post(API_URL + "/date")
                .headerMap(authMap(), false)
                .body(JSONUtil.toJsonStr(start))
                .execute()
                .body();
    }

    public String randomDateByGet(Integer start) {
        return HttpRequest.get(API_URL + "/date?start=" + start)
                .body(JSONUtil.toJsonStr(start))
                .headerMap(authMap(), false)
                .execute()
                .body();
    }


    public static ApiClient build(String accessKey, String secretKey) {
        setAccessKey(accessKey);
        setSecretKey(secretKey);
        return INSTANCE;
    }

    private static void setAccessKey(String accessKey) {
        INSTANCE.accessKey = accessKey;
    }

    private static void setSecretKey(String secretKey) {
        INSTANCE.secretKey = secretKey;
    }

}
